<?php
require_once("../../../config.inc.php");
$isReloadable = false;
require_once("../../../app/_inc/include.php");
require_once("../_inc/_mbFlagProfValue.php");
require_once("profileFunction.php");

$errMsg = "";
$id_profile = sxREQUEST("id_profile");
if(isset($_REQUEST["reqtype"]) && $_REQUEST["reqtype"] == "save"){
	$errMsg .= profileUserIns();	
}
if(isset($_REQUEST["id"])){
	$id_profile = $_REQUEST["id"];
}
$aFlagProfile = array();
$descrProfile = "";
if($id_profile != ""){
	//recupero la descrizoine del profilo
	$sSQL = "SELECT description FROM profile WHERE id_profile=$id_profile";
	if($objSDAO->query($sSQL, "descrPro", array("contOnErr"=>true))){		
		if($row = $objSDAO->fetch("descrPro")){
			$descrProfile = $row["description"];
		}
	}else{
		$errMsg .= $sSQL." - ".$objSDAO->error;
	}
	$sSQL = "SELECT u.*, up.id_user as id_user_profile FROM user u " .
			"LEFT OUTER JOIN user_profile up " .
			"ON u.id_user=up.id_user " .
			"AND id_profile=$id_profile ORDER BY u.last_name, u.name";
	if(!$objSDAO->query($sSQL, "selUser", array("contOnErr"=>true))){		
		$errMsg .= $sSQL." - ".$objSDAO->error;
	}
}else{
	$errMsg.= _t("profilo non definito")."<br>";
}
//output HTML
$aOpz = array("tit2"=>_t("Utenti del profilo"), "isReloadable"=>false);
echo(headerHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript" src="../../_jsc/ajax.js"></script>  
<script language="JavaScript" type="text/javascript">
	function objFrmSave(){
		var errMsg = "";
		var objFrm = document.getElementById("objFrm");
		var objMessArea =  document.getElementById("messArea");
		if(errMsg == ""){
			objFrm.submit();
		}else{
			objMessArea.innerHTML = errMsg;
		}		
	}
</script>
  
<?php
echo(bodyHTML($aOpz));
echo("<table width=\"100%\" cellpadding=\"03\" cellspacing=\"0\">\n<tr>\n<td>\n");
//form inserimento dati
echo("<form name=\"objFrm\" id=\"objFrm\" action=\"profileUserIns.php\" method=\"POST\">");
echo("<input type=\"hidden\" name=\"xsn\" value=\"".XSN."\">");
echo("<input type=\"hidden\" name=\"reqtype\" id=\"reqtype\" value=\"save\">");
echo("<input type=\"hidden\" name=\"id_profile\" id=\"id_profile\" value=\"$id_profile\">");
//output comandi
$aCmd = array("ins"=>array("pag"=>"javascript:objFrmSave()",
										"title"=>_t("Salva i dati del profilo"),
										"text"=>_t("salva"),
										"perm"=>"updpro"),
					"back"=>array("pag"=>$objUser->getHistory(-1,false),
										"title"=>$objUser->getHistoryText(-1),
										"text"=>$objUser->getHistoryText(-1))
					);
echo(commandsHTML($aCmd));	
echo("<table class='tabl01' width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
echo("<tr>" .
			"<td class=\"titl01\"colspan=\"4\">"._t("Utenti associati al profilo")."</td>" .			
			
		"</tr>\n");
echo("<tr>" .
			"<td class=\"tabh01\" width=\"5%\">&nbsp;</td>" .			
			"<td class=\"tabh01\" width=\"5%\">"._t("utente")."</td>" .
			"<td class=\"tabh01\" width=\"20%\">"._t("nome")."</td>" .
			"<td class=\"tabh01\" width=\"65%\">&nbsp;</td>" .
		"</tr>\n");

while($row = $objSDAO->fetch("selUser")){
	$chk = "";
	if($row["id_user"] == $row["id_user_profile"])
		$chk = "CHECKED";
	echo("<tr>" .
				"<td class=\"labl01\"><input type=\"checkbox\" id=\"user_".$row["id_user"]."\" name=\"user_".$row["id_user"]."\" $chk></td>" .
				"<td class=\"text01\">".$row["uid"]."</td>" .
				"<td class=\"text01\">".sxHE($row["name"])." ".sxHE($row["last_name"])."</td>" .
				"<td class=\"text01\">&nbsp;</td>" .
			"</tr>\n");
	}
echo("</table>");

echo(commandsHTML($aCmd));
echo("</form>");
echo("</td>\n</tr>\n</table>\n");
echo(footerHTML());
$objSDAO->close();
?>
